import { ColumnType } from 'antd/lib/table'
import { UserInfo } from '@src/components/schema/basic'
import { awaitWrap, confirmAction, formatDate, limitStr } from '@js/tool'
import moment from 'moment'
import { Space, message } from 'antd'
import { delRequest } from './ajax'
import { SystemQuestionSchema } from '@src/components/schema/systemQuestion'

async function delHandle (id: string, refreshFn: () => void): Promise<void> {
  const hide = message.loading('删除中', 60)
  const [e] = await awaitWrap(delRequest(id))
  hide()
  if (e === null) {
    message.success('删除成功').then(null, null)
    refreshFn()
  }
}

export function getColumn (userInfo: UserInfo, refreshFn: () => void, setModifyData: (v: SystemQuestionSchema) => void, setDetailVisible: (v: boolean) => void, setEditVisible: (v: boolean) => void, setAdminVisible: (v: boolean) => void): Array<ColumnType<any>> {
  const isAdmin = userInfo.roleid_default === 'sysadmin'
  const columns = [
    {
      title: '序号',
      dataIndex: 'orderno',
      key: 'orderno',
      // align: 'center',
      width: 60
    },
    {
      title: '标题',
      dataIndex: 'title',
      key: 'title'
    },
    {
      title: '问题描述',
      dataIndex: 'content',
      key: 'content',
      render: (v: string) => {
        return limitStr(v)
      }
    },
    {
      title: '创建时间',
      dataIndex: 'create_datetime',
      key: 'create_datetime',
      render: (v: number) => {
        return v !== undefined ? moment(v * 1000).format('YYYY-MM-DD') : ''
      }
    },
    {
      title: '创建人',
      dataIndex: 'operator',
      key: 'operator'
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      width: 100
    },
    {
      title: '处理时间',
      dataIndex: 'complete_datetime',
      key: 'complete_datetime',
      width: '110px',
      render: (v: number) => {
        if (typeof v === 'number') {
          return formatDate(v)
        }
      }
    },
    {
      title: '处理结果',
      dataIndex: 'remark',
      key: 'remark',
      render: (v: string) => {
        return limitStr(v)
      }
    },
    {
      title: '操作',
      dataIndex: 'index',
      key: 'operate',
      width: 200,
      render: (value: any, line: SystemQuestionSchema) => {
        return (
          <Space>
            {line.status === '提交' && !isAdmin && <a onClick={() => { setModifyData(line); setEditVisible(true) }}>编辑</a>}
            {isAdmin && <a onClick={() => { setModifyData(line); setAdminVisible(true) }}>编辑</a>}
            <a onClick={() => { setModifyData(line); setDetailVisible(true) }}>详情</a>
            {line.status === '提交' && <a onClick={() => confirmAction('您确定要删除吗？', async () => await delHandle(line.id, () => refreshFn()))}>删除</a>}
          </Space>
        )
      }
    }
  ]
  return columns
}
